/*
 * This file is part of Plop Messenger.
 *
 * Plop Messenger is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  Plop Messenger is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Plop Messenger.  If not, see <http://www.gnu.org/licenses/>.
 */
package pm.ui;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;

import pm.Constantes;
import pm.util.Msg;
import pm.util.Options;

public class MainFrame extends JFrame implements WindowListener {

	private static final long serialVersionUID = 1L;

	private Options options;
	private JTabbedPane tabbedPane = new JTabbedPane();

	public MainFrame(Options options) {
		super(Msg.get("pm.window.title"));
		this.options = options;

		this.add(tabbedPane);
		this.pack();
		this.setSize(options.getInt("window.width", 500), options.getInt(
				"window.height", 400));
		if (options.exists("window.x") && options.exists("window.y"))
			this.setLocation(options.getInt("window.x", 0), options.getInt(
					"window.y", 0));
		else
			this.setLocationRelativeTo(null);
		this.addWindowListener(this);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void addChanelPanel(String titre, ChanelPanel chanelPanel) {
		tabbedPane.addTab(titre, chanelPanel);
	}

	public void removeChanelPanel(ChanelPanel chanelPanel) {
		tabbedPane.remove(chanelPanel);
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent e) {
		if (e.getSource() == this) {
			options.setInt("window.width", this.getWidth());
			options.setInt("window.height", this.getHeight());
			options.setInt("window.x", this.getX());
			options.setInt("window.y", this.getY());
			try {
				options.saveAs(Constantes.OPTIONS_FILE);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

}
